#!  /usr/bin/env python
# ecoding=utf-8

import redis
import json
from datetime import date, datetime
from ..setting import *

rds = redis.StrictRedis(host=REDIS_IP, port=REDIS_PORT,password=REDIS_PASSWORD,decode_responses=True)
template_urls_queues = bytes('myspider:template_urls_queues','utf-8')

class CJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)
class TemplateUrlQueueDao():
    @staticmethod
    def lpush_list(list):
        json_objs=[]
        for json_str in list:
            json_obj=json.dumps(json_str,cls=CJsonEncoder)
            json_objs.append(json_obj)
        rds.lpush(template_urls_queues,json_objs)
    @staticmethod
    def lpush(json_str):
        json_obj=json.dumps(json_str,cls=CJsonEncoder)
        rds.lpush(template_urls_queues,json_obj)
    @staticmethod
    def qsize():
        return rds.llen(template_urls_queues)

    @staticmethod
    def exist(val):
        exist=False
        for value in rds.lrange(template_urls_queues,0,-1):
            if value==val:
                exist=True;
                break;
        return exist;

